<?php


namespace app\agent\controller;

class AuthApp extends AdminBase
{
	public $project = [];
	public $fixed_options = [];
	public function __construct()
	{
		parent::__construct();
		$appid = input("appid");
		if (empty($appid)) {
			$this->response(\app\agent\model\Code::fail, "缺少appid参数");
		}
		$app = \think\Db::name("app")->where(["id" => $appid])->find();
		if (empty($app)) {
			$this->response(\app\agent\model\Code::fail, "appid不存在");
		}
		if (!\think\Db::name("app_agent_binding")->where(["agent_id" => $this->admin_info["id"], "appid" => $appid])->find()) {
			$this->response(\app\agent\model\Code::fail, "未授权该应用");
		}
		$this->project = $app;
		if ($this->project["type"] != 2) {
			$this->response(\app\agent\model\Code::fail, "当前应用类型错误不符合条件");
		}
		$this->fixed_options["appid"] = $appid;
		$this->fixed_options["agent_id"] = $this->admin_info["id"];
		$this->fixed_options["limit"] = $this->page_size;
		$this->fixed_options["page"] = $this->page_no;
	}
	public function get_card_list()
	{
		$options = array_merge($this->request->get(), $this->fixed_options);
		$result = \app\agent\service\AuthAppService::get_card_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_card_info($id)
	{
		$options = array_merge(["id" => $id], $this->fixed_options);
		$result = \app\agent\service\AuthAppService::get_card_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_card()
	{
		$options = array_merge($this->request->post(), $this->fixed_options);
		$validate = \think\Validate::make(["appid" => "require", "type" => "require", "num" => "require|number|>=:1"], ["appid.require" => "appid 不能为空", "type.require" => "类型 不能为空", "num.require" => "请输入数量"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		if ($options["type"] === 0) {
			if (!isset($options["time_type"]) || $options["time_type"] === "") {
				$this->response(\app\agent\model\Code::fail, "请选择时长类型");
			}
			if (empty($options["value"])) {
				$this->response(\app\agent\model\Code::fail, "请输入时长");
			}
		}
		if ($options["type"] === 1 && empty($options["value"])) {
			$this->response(\app\agent\model\Code::fail, "请输入面值");
		}
		$result = \app\agent\service\AuthAppService::add_card($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_card_batch_list()
	{
		$options = array_merge($this->request->get(), $this->fixed_options);
		$result = \app\agent\service\AuthAppService::get_card_batch_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]], ["all_card_data", $result["all_card_data"]]);
	}
	public function export_card()
	{
		// 关闭错误显示，防止 PHPExcel 的 Deprecated 警告污染 JSON 响应
		error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

		$options = array_merge($this->request->post(), $this->fixed_options);
		$validate = \think\Validate::make(["appid" => "require", "batch_id" => "require", "export_file_type" => "require|in:txt,excel"], ["appid.require" => "appid 不能为空", "batch_id.require" => "batch_id 不能为空", "export_file_type.require" => "export_file_type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size_max;
		$options["page"] = 1;
		$result = \app\agent\service\AuthAppService::export_card_user_table($options);
		if ($result["code"] == \app\agent\model\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function delete_card()
	{
		$options = array_merge($this->request->post(), $this->fixed_options);
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$result = \app\agent\service\AuthAppService::delete_card($options);
		if ($result["code"] == \app\agent\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function delete_card_batch()
	{
		$options = array_merge($this->request->post(), $this->fixed_options);
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$result = \app\agent\service\AuthAppService::delete_card_batch($options);
		if ($result["code"] == \app\agent\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}